#include "helicopter.h"
#include "terrain.h"
#include "player.h"

void Helicopter::OnCollision(CObject *collisionObject)
{
	// if this enemy collides with the terrain (always)
	if (typeid(*collisionObject) == typeid(CTerrain))
	{
		position.y = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size/2;
		if (position.x <= ((CTerrain*)collisionObject)->GetScanDepth())
			position.x = ((CTerrain*)collisionObject)->GetScanDepth();
		if (position.x >= ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth())
			position.x = ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth();
		if (position.z <= ((CTerrain*)collisionObject)->GetScanDepth())
			position.z = ((CTerrain*)collisionObject)->GetScanDepth();
		if (position.z >= ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth())
			position.z = ((CTerrain*)collisionObject)->GetWidth()*((CTerrain*)collisionObject)->GetMul() - ((CTerrain*)collisionObject)->GetScanDepth();
	}
}

void Helicopter::OnPrepare() 
{	
	float distFromPlayer;
	// calculate distance from player
	CVector diff = player->position - position;
	// Get distance to player
	distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);

	if(distFromPlayer < 300)
		//if((clock() - start) / CLOCKS_PER_SEC > 1)
		//{
			if(distFromPlayer < 75)
			{
				if(!isHelicopterPlaying)
				{
					audioSys->StopSoundDX(audioSys->iHelicopter_med);
					audioSys->PlaySoundDX(audioSys->iHelicopter);
					isHelicopterMedPlaying = false;
					isHelicopterPlaying = true;
				}
			}
			else if(distFromPlayer < 200)
			{
				if(!isHelicopterMedPlaying)
				{
					audioSys->StopSoundDX(audioSys->iHelicopter);
					audioSys->StopSoundDX(audioSys->iHelicopter_low);
					audioSys->PlaySoundDX(audioSys->iHelicopter_med);
					isHelicopterMedPlaying = true;
					isHelicopterLowPlaying = false;
					isHelicopterPlaying = false;
				}
				
			}
			else // distance is < 300
			{
				if(!isHelicopterLowPlaying)
				{
					audioSys->StopSoundDX(audioSys->iHelicopter_med);
					audioSys->PlaySoundDX(audioSys->iHelicopter_low);
					isHelicopterMedPlaying = false;
					isHelicopterLowPlaying = true;
				}
			}
			// else, don't play sound
			start = clock();
		//}

	CEntity::OnPrepare();
}

void Helicopter::Load()
{
	// load model
	CMD2Model::Load("..\\..\\res\\models\\helicopter\\helicopter.md2", "..\\..\\res\\models\\helicopter\\helicopter.pcx");
}

void Helicopter::OnDraw(CCamera *camera)
{
	// if the rocket has not yet exploded, draw the rocket model
		glEnable(GL_TEXTURE_2D);
		glColor3f(1.0, 1.0, 1.0);
		glTranslatef(position.x, position.y+11, position.z);
		glScalef(8.0f, 8.0f, 8.0f);
		glRotatef(-90, 1.0, 0.0, 0.0);
		RenderFrame(0);
		glDisable(GL_TEXTURE_2D);
}
